# Maintainer: xduugu
# Contributor: nh2
# Contributor: Steven Davidovitz <steviedizzle ð gmail đ com>
# Contributor: Nick B <Shirakawasuna ð gmail đ com>
# Contributor: Christof Musik <christof ð senfdax đ de>
# Contributor: Stefan Rupp <archlinux ð stefanrupp đ de>
# Contributor: Ignas Anikevicius <anikevicius ð gmail đ com>

_pkgname=tp_smapi
_kernext=
pkgname=$_pkgname$_kernext
_extramodules=extramodules-3.1-ARCH
_kernver="$(cat /lib/modules/${_extramodules}/version)"
pkgver=0.41
pkgrel=3
pkgdesc="Modules for ThinkPad's SMAPI functionality"
arch=('i686' 'x86_64')
url="http://tpctl.sourceforge.net/"
license=('GPL')
depends=("linux$_kernext" "linux>=3.1" "linux<3.2")
makedepends=("linux$_kernext-headers" )
install=$_pkgname.install
source=(https://cloud.github.com/downloads/evgeni/$_pkgname/$_pkgname-$pkgver.tar.gz) # https://github.com/downloads/evgeni/$_pkgname/$_pkgname-$pkgver.tar.gz
md5sums=('63c683415c764568f6bf17c7eabe4752')
sha256sums=('6aef02b92d10360ac9be0db29ae390636be55017990063a092a285c70b54e666')

build() {
  cd "$srcdir/$_pkgname-$pkgver" || return 1
  make HDAPS=1 || return 1
  #make HDAPS=1 FORCE_IO=1 || return 1
}

package() {
    #mkdir -p "${pkgdir}/lib/modules/${_extramodules}" || return 1
    install -D -m644 "${srcdir}/${_pkgname}-${pkgver}/hdaps.ko" \
        "${pkgdir}/lib/modules/${_extramodules}/hdaps.ko" || return 1

    install -D -m644 "${srcdir}/${_pkgname}-${pkgver}/thinkpad_ec.ko" \
        "${pkgdir}/lib/modules/${_extramodules}/thinkpad_ec.ko" || return 1

    install -D -m644 "${srcdir}/${_pkgname}-${pkgver}/tp_smapi.ko" \
        "${pkgdir}/lib/modules/${_extramodules}/tp_smapi.ko" || return 1

  #make SYSSRC=/lib/modules/"${_kernver}/build" module
  #make -C "/lib/modules/$(uname -r)/build" INSTALL_MOD_PATH="$pkgdir" M="$srcdir/$_pkgname-$pkgver" modules_install
  #make -C "/lib/modules/${_kernver}/build" INSTALL_MOD_PATH="$pkgdir" M="$srcdir/$_pkgname-$pkgver" modules_install
  # move hdaps.ko into updates to override in-tree kernel module
  #cd "$pkgdir/lib/modules/$(uname -r)/"
  #mkdir updates
  #mv extra/hdaps.ko updates

  # compress kernel modules
  find "$pkgdir" -name "*.ko" -exec gzip -9 {} +

  # update kernel version in install file
  #sed -ri "s/^(kernver=).*$/\1$(uname -r)/" "$startdir/$_pkgname.install"
  sed -i -e "s/EXTRAMODULES='.*'/EXTRAMODULES='${_extramodules}'/" "${startdir}/${_pkgname}.install"
}
